using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace GameWP
{
    public class UpgradeLuffyComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game _game;
        private SpriteBatch _spriteBatch;
        private SpriteFont _textFont;
        private Vector2 touchPosition, pretouchPosition;
        private int _price4Up;
        Button _UpLevel, _price, _infoAfterUpLevel;

        //Initilation
        public UpgradeLuffyComponent(Game game, SpriteFont textFont, SpriteBatch spriteBatch, Texture2D textureUpLevel)
            : base(game)
        {
            _game = game;
            _textFont = textFont;
            _spriteBatch = spriteBatch;

            //Init Button
            int levelup = ManagerGame.luffy.Level + 1;
            _price4Up = ((int)(levelup * Math.Pow(1.2, levelup) * 148 * 2));
            _UpLevel = new Button(textureUpLevel, new Vector2(240, 190));
                _UpLevel.Selected += new Button.IsSelected(_UpLevel_Selected);
            _price = new Button("", textFont, new Vector2(350, 230),Color.Yellow){HaveAnimation = false};
            string _info = "Lv " + (levelup - 1).ToString() + " --> Lv " + levelup.ToString() + 
                ":\n     - Hp   : " + ManagerGame.luffy.Hp.ToString() + " --> " + ((int)(ManagerGame.luffy.Hp*1.2)).ToString() + 
                "\n     - Mana: " + ManagerGame.luffy.Mana.ToString() + " --> " + ((int)(ManagerGame.luffy.Mana*1.1)).ToString() + 
                "\n     - Dam : " + ManagerGame.luffy.Damage.ToString() + " --> " + ((int)(ManagerGame.luffy.Damage*1.25)).ToString();
            _infoAfterUpLevel = new Button(_info, _textFont, new Vector2(30, 280), Color.White) { HaveAnimation = false };
        }

        void _UpLevel_Selected(int input)
        {
            // Kiem tra tien ....
            if (ManagerGame.luffy.Balance >= _price4Up)
            {
                ManagerGame.luffy.Up2Level();
                ManagerGame.luffy.Balance -= _price4Up;                
            }
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
                base.Initialize();
        }


        public override void Update(GameTime gameTime)
        {
            int levelup = ManagerGame.luffy.Level + 1;
            _price4Up = ((int)(levelup * Math.Pow(1.2, levelup) * 148 * 2));
            string _info = "Lv " + (levelup - 1).ToString() + " --> Lv " + levelup.ToString() +
            ":\n     - Hp   : " + ManagerGame.luffy.Hp.ToString() + " --> " + ((int)(ManagerGame.luffy.Hp * 1.17)).ToString() +
            "\n     - Mana: " + ManagerGame.luffy.Mana.ToString() + " --> " + ((int)(ManagerGame.luffy.Mana * 1.1)).ToString() +
            "\n     - Dam : " + ManagerGame.luffy.Damage.ToString() + " --> " + ((int)(ManagerGame.luffy.Damage * 1.2)).ToString();
            _infoAfterUpLevel.Text = _info;
            // TODO: Add your update code here    
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                _game.Exit();
            //Key input
            if (TouchPanel.IsGestureAvailable)
            {
                // Read in the Gesture input
                GestureSample gesture = TouchPanel.ReadGesture();
                // Process Gestures
                if (gesture.GestureType == GestureType.Tap)
                {
                    _UpLevel.ButtonIsTouched(gesture.Position, 0);            
                }
            }
            
            _price.Text = "$" + _price4Up.ToString();

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            _UpLevel.Draw(_spriteBatch, gameTime);
            _price.Draw(_spriteBatch, gameTime);
            _infoAfterUpLevel.Draw(_spriteBatch, gameTime);
        }
    }
}
